﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL.Classes;
using System.IO;
using DAL.Help;
using System.Data.SqlClient;
using System.Drawing;

public partial class CapNhatBaiDang : System.Web.UI.Page
{
    #region Load du lieu len web
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["MaTaiKhoan"] == "" && Session["QuyenHan"].ToString() == "")
            {
                Response.Redirect("~/DangNhap.aspx");
            }
            if (ConvertType.ToInt32(Session["QuyenSuDung"]) == 2)
            {
                Response.Redirect("~/TrangCaNhan.aspx?TaiKhoanID=" + Session["MaTaiKhoan"]);
            }
            else
            {
                PopulateControls();
            }
        }
    }
    //Load ddl Menu
    private void LoadMenu()
    {
        ddlMenuID.DataValueField = "MaMenu";
        ddlMenuID.DataTextField = "TenMenu";
        ddlMenuID.DataSource = Menus.MenuAll();
        ddlMenuID.DataBind();
    }
    //Load ddl LoadCategory
    private void LoadCategory(string menuID)
    {
        ddlCategory.Items.Clear();
        ddlCategory.DataValueField = "MaChuDe";
        ddlCategory.DataTextField = "TenChuDe";
        ddlCategory.DataSource = ChuDe.LayChuDeTheoMenu(menuID);
        ddlCategory.DataBind();
    }
    private void LoadTheLoai()
    {
        ddlTheLoai.Items.Clear();
        ddlTheLoai.DataValueField = "MaTheLoai";
        ddlTheLoai.DataTextField = "TenTheLoai";
        ddlTheLoai.DataSource = TheLoai.LayTatCa();
        ddlTheLoai.DataBind();
    }
    //Load
    private void PopulateControls()
    {
        //Load ddl Menu
        LoadMenu();
        LoadTheLoai();
    }

    protected void btnLoc_Click(object sender, EventArgs e)
    {
        //Menus data = ddlMenuID.SelectedItem as Menus;
        string menuID = ddlMenuID.SelectedValue;
        if (menuID != null)
            LoadCategory(menuID);
    }
    #endregion

    #region Xu kien luu anh
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        btnUpload.Click += new EventHandler(btnUpload_Click);
    }

    bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".gif":
                return true;
            case ".png":
                return true;
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            default:
                return false;
        }
    }

    void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            // Kiem tra co file upload va kich thuoc file <= 500KB 
            if (upImage.HasFile && upImage.PostedFile.ContentLength <= 500000)
            {
                // Kiem tra loai file 
                if (CheckFileType(upImage.FileName))
                {
                    // Lay duong dan tuong doi o server 
                    String filePath = "~/HinhAnh/" + upImage.FileName;
                    txtPath.Text = upImage.FileName;
                    // Luu vao duong dan o tren 
                    // MapPath(filePath) ==> chuyen ve duong dan tuyet doi 
                    upImage.SaveAs(MapPath(filePath));
                    lblStatus.Text = "Upload thành công!";
                    //PopulateControls();
                }
            }
            else
            {
                lblStatus.Text = "Upload không thành công!";
            }
        }

        catch (Exception)
        {
            lblStatus.Text = "Upload không thành công!";
        }
    }
    #endregion

    #region Luu Album
    private BaiDang GetData()
    {
        BaiDang data = new BaiDang();
        data.MaChuDe = Convert.ToInt32(ddlCategory.SelectedValue);
        data.MaTheLoai = Convert.ToInt32(ddlTheLoai.SelectedValue);
        data.TenBaiDang = txtTenAlbum.Text;
        data.NgayTao = DateTime.Now;
        data.MoTa = txtMoTa.Text;
        data.NoiDung = txtPath.Text;
        data.MaTaiKhoan = Convert.ToInt32(Session["MaTaiKhoan"]);
        data.TrangThai = 1;
        return data;
    }
    protected void btnLuuAlbum_Click(object sender, EventArgs e)
    {
        try
        {
            bool rs = false;
            BaiDang data = GetData();
            rs = BaiDang.Them(data);
            if (rs)
            {
                BaiDang ab = BaiDang.LayMaBaiDangMax();
                int albumImagesIDMax = ConvertType.ToInt32(ab.MaBaiDang.ToString());
                Response.Redirect("~/CapNhatThongTinBaiDang.aspx?BaiDangID=" + albumImagesIDMax);
            }
            else
                lbCreateAlbum.Text = "Tạo Album không thành công!";
        }
        catch (SqlException ex)
        {
            lbCreateAlbum.Text = ex.Message.ToString();
        }
    }
    protected void txtTenAlbum_TextChanged(object sender, EventArgs e)
    {
        if (txtTenAlbum.Text.Length > 200)
        {
            lbTenAlbum.Visible = true;
            lbTenAlbum.Text = "(<200 ký tự)";
        }
        else
            lbTenAlbum.Visible = false;
    }
    protected void txtMoTa_TextChanged(object sender, EventArgs e)
    {
        if (txtMoTa.Text.Length > 500)
        {
            lbMoTa.Visible = true;
            lbMoTa.Text = "(<500 ký tự)";
        }
        else
            lbMoTa.Visible = false;
    }
    #endregion
}
